
package com.mc.lp.database.service.handler;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import com.mc.lp.database.entity.MicroCampusEntity;
import com.mc.lp.database.entity.helper.MicroCampusHelper;
import com.mc.lp.database.service.handler.constant.FormRequestType;
import com.mc.lp.database.service.handler.constant.SearchParam;
import com.mc.lp.service.MicroCampusEntityService;
import com.mc.lp.shared.util.constants.ResponseStatus;
import com.mc.lp.shared.vo.IValueObject;
import com.mc.lp.shared.vo.ServiceResponse;
import com.mc.lp.shared.vo.StudentProfileVO;
import com.mc.lp.shared.vo.UtilServiceVO;
/**
 * search handler to search difference client requests.
 * @author deepak
 *
 */
public class SearchHandler extends AbstractFormHandler<IValueObject, IValueObject> {
	@Resource
	private MicroCampusEntityService microCampusEntityService;
	
	@Override
	public HashSet<FormRequestType> getRequestHandlingType() {
		HashSet<FormRequestType> handledRequest = new HashSet<FormRequestType>();
		handledRequest.add(FormRequestType.STUDENT_SEARCH);
		handledRequest.add(FormRequestType.GET_STUDENT_INFO);
		return handledRequest;
	}

	@Override
	public IValueObject serveRequest(FormRequestType requestType, IValueObject object) throws Exception {
		switch (requestType) {
		case STUDENT_SEARCH:
			return getSearchStudentResult(object);
		case GET_STUDENT_INFO:
			return getStudentInfo(object);
		default:
			ServiceResponse<IValueObject> defaultResult= new ServiceResponse<IValueObject>();
			defaultResult.setStatus(ResponseStatus.NOT_FOUND);
			return defaultResult;
		}
	}
	/*
	 * return student information based on its given student id
	 */
	private IValueObject getStudentInfo(IValueObject object){
		UtilServiceVO vo = (UtilServiceVO) object;
		ServiceResponse<StudentProfileVO> studentSearchResult= new ServiceResponse<StudentProfileVO>();
		List<StudentProfileVO> studentProfile = new ArrayList<StudentProfileVO>();
		if(vo.getSearchCriteria() != null){
			StudentProfileVO profile = microCampusEntityService.getStudentProfileById(vo.getSearchCriteria().getStudentEntityId());
			if(profile!=null){
				studentProfile.add(profile);
				studentSearchResult.setResult(studentProfile);
				studentSearchResult.setStatus(ResponseStatus.SUCCESS);
				return studentSearchResult;
			}
		}
			studentSearchResult.setStatus(ResponseStatus.FAILED);
			studentSearchResult.setMsg("Student Search Criteria Not found");
			return studentSearchResult;
	}
	
	private IValueObject getSearchStudentResult(IValueObject object){
		
		//DV DONE: Implemented with all the search params
		UtilServiceVO vos = (UtilServiceVO) object;
		SearchParam searchParam=SearchParam.getType(Long.valueOf(vos.getSearchCriteria().getSearchParameter()));
		String searchValue=vos.getSearchCriteria().getSearchQuery();
		
		Set<StudentProfileVO> studentList = microCampusEntityService.getEntitiesBySearchParam(searchParam, searchValue);
		List<StudentProfileVO> studentProfiles = new ArrayList<StudentProfileVO>(studentList);

		ServiceResponse<StudentProfileVO> studentSearchResult= new ServiceResponse<StudentProfileVO>();
		studentSearchResult.setStatus(ResponseStatus.SUCCESS);
		studentSearchResult.setResult(studentProfiles);
		return studentSearchResult;
	}
}
